//
//  NSString+DSHex.m
//  KX11UI
//
//  Created by HYC on 2021/2/19.
//

#import "NSString+Tools_HYC_Neusoft.h"

@implementation NSString (Tools_HYC_Neusoft)


- (UIColor *)color {
    
    NSString *cString = self;//[[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6)
    {
        return [UIColor clearColor];
    }
    // strip 0X if it appears
    //如果是0x开头的，那么截取字符串，字符串从索引为2的位置开始，一直到末尾
    if ([cString hasPrefix:@"0X"])
    {
        cString = [cString substringFromIndex:2];
    }
    //如果是#开头的，那么截取字符串，字符串从索引为1的位置开始，一直到末尾
    if ([cString hasPrefix:@"#"])
    {
        cString = [cString substringFromIndex:1];
    }
    if ([cString length] != 6)
    {
        return [UIColor clearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:1];
}
- (UIImage *)image {
    NSString *cString = self;
    return [UIImage imageNamed:cString];
    
//    NSString *imgName = [NSString stringWithFormat:self];
//    return vehicleApp(imgName);
    
}
- (UIFont *)font {
    return [UIFont systemFontOfSize:self.floatValue];
}
- (UIFont *)weight:(UIFontWeight)we {
    return [UIFont systemFontOfSize:self.floatValue weight:we];
}
- (NSString *)fromDate:(NSTimeInterval )date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    [dateFormatter setDateFormat:self];
//    double d            = date;
    NSDecimalNumber *dStr      = [[NSDecimalNumber alloc]initWithFloat:date];
    
    return [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:dStr.stringValue.length == 13 ? (date) / 1000 : (date)]];

}
- (NSTimeInterval )timeInterval:(NSString * )date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    //zzz表示时区，zzz可以删除，这样返回的日期字符将不包含时区信息。
    [dateFormatter setDateFormat:self];
//    double d            = date;
//    NSString *dStr      = [NSString stringWithFormat:@"%f", d];

    
    return ([[dateFormatter dateFromString:date] timeIntervalSince1970] * 1000);

}
- (NSURL *)urlImage {
    return [NSURL URLWithString:self];
}
- (void)Call {
    NSMutableString* str = [[NSMutableString alloc]initWithFormat:@"telprompt://%@",self];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
}
- (NSString *)Error_description {
    
    
    NSDictionary *errorDic = @{
    @"1400": @"请求参数错误",
    @"1402": @"请求没有权限",
    @"1405": @"请求资源不存在",
    @"1500": @"系统内部错误",
    @"1501": @"数字钥匙不存在",
    @"1600": @"车辆数字钥匙未激活，请激活",
    @"1601": @"人车关系不存在",
    @"1604": @"第三方交互异常",
    @"1605": @"根据vin查询车辆详情数据返回异常",
    @"1606": @"车辆数字钥匙禁止此操作,设备ID不一致",
    @"1607": @"指令执行失败,车辆不在线或其他情况!",
    @"1800": @"Call Grpc error",
    @"18000": @"PkiCertCarNotExist",
    @"18001": @"PkiCertAppNotExist",
    @"18002": @"PkiCertCsrError",
    @"18003": @"PkiCertAppNotExist",
    @"2000": @"下发钥匙失败",
    @"2001": @"Call Grpc error",
    @"2002": @"数据库操作异常",
    @"2003": @"数字钥匙已存在",
    @"2004": @"分享数字钥匙已存在",
    @"2005": @"数字钥匙不存在",
    @"2006": @"车辆钥匙未同步",
    @"2007": @"车辆钥匙已同步",
    @"2008": @"认证钥匙更新信息失败",
    @"2009": @"认证钥匙失败",
    @"2010": @"删除车辆钥匙失败",
    @"2011": @"钥匙同步失败",
    @"2012": @"蓝牙钥匙分享超过最大次数10次",
    @"2013": @"分享类型错误",
    @"2014": @"设备来源类型错误只能为[1、3]其中值",
    @"2015": @"分享次数[1-10] 之间",
    @"2016": @"钥匙信息不匹配",
    @"2017": @"车主设备来源范围[1、2、3、4、5]其中值",
    @"2018": @"设备信息错误",
    @"2019": @"NFC CARD 或NFC WATCH数量超限",
    @"2020": @"手表钥匙只能分享一次",
    @"2021": @"手机NFC钥匙只能分享一次",
    @"2022": @"数字钥匙已过期",
    @"2023": @"booking.client初始化失败",
    @"2024": @"secure.client初始化失败",
    @"2025": @"蓝牙、手表、nfc 只能有一把车主钥匙",
    @"2026": @"设备id一致无需认证",
    @"2027": @"调用NFC服务异常",
    @"2028": @"分享类型不符合要求",
    @"2029": @"非蓝牙钥匙无需认证",
    @"2030": @"设备Id不存在",
    @"2031": @"车辆vin码不存在",
    @"2032": @"CmacKey 不存在",
    @"2033": @"超过最大分享次数且只能为5把",
    @"2034": @"NFC数据验签失败",
    @"2035": @"获取车辆钥匙失败",
    @"3001": @"请先配置VDS 版本",
    @"3000": @"钥匙同步失败",
    @"3002": @"call Grpc error",
    @"3003": @"Needn't send because powerMode 2 is not allow",
    @"3004": @"Needn't send because powerMode 3 is not allow",
    @"3005": @"Needn't send because powerMode 1 is not allow",
    @"3006": @"secure.client初始化失败",
    @"3007": @"调用加密cemdk.error",
    @"3008": @"parameters invalid",
    @"3009": @"Priority error",
    @"16004": @"车辆回复失败",
    @"4000": @"数字钥匙下发成功",
    @"4001": @"钥匙数量已满",
    @"4002": @"时间同步错误",
    @"4003": @"数据异常",
    @"4004": @"未知原因",
    @"4005": @"电源模式错误",
    @"4006": @"整车电源异常",
    @"4007": @"数字钥匙没有使能",
    @"4008": @"时间超出范围",
    @"4009":@"删除成功",
    @"4010":@"车辆在行驶",
    @"4011":@"车是解锁状态",
    @"4012":@"车窗未关",
    @"4013":@"车辆非禁止状态",
    @"4014":@"无效BookingId",
    @"4015":@"内部错误",
    @"4016":@"电源模式非normal",
    @"4017":@"电压超出范围",
    @"4018":@"数字钥匙功能禁用",
    @"4019": @"NFC CARD Rejected due to card is locked",
    @"4020": @"NFC CARD Rejected due to card(received failed status from card)",
    @"4021": @"NFC CARD Rejected due to communiation timeout with card",
    @"4022": @"NFC CARD Rejected due to Invalid data received(booking ID does not exist)",
    @"4023": @"NFC CARD Rejected due todata wrote to cardfailed(received status from card)",
    @"4024": @"NFC CARD Rejected due to DigKeyMgr general internal error",
    @"4025": @"NFC CARD Rejected due to CarMod or UsgMod",
    @"4026": @"NFC CARD Rejected due to VBatt",
    @"4027": @"NFC CARD Rejected due to DK Function is Disabled",
    @"4028": @"Please unlock card",
    @"4029": @"Please write card",
    @"4030": @"Please add pass",
    @"4031": @"Please auth applet",
    @"4032": @"NFC Call Nfc failed",
    @"19005": @"SecureErrorNFCKeyNotFound",
    @"19006": @"SecureErrorNFCIVNotFound",
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    @"1803" : @"当前车辆暂不支持车分享",
    @"1804" : @"错误的角色类型",
    @"17001" :@"当前分享状态不支持进行该操作",
    @"17002" :@"当前的车辆有正在进行的分享，暂时无法创建新的分享",
    @"17004" :@"开始取车时间格式错误",
    @"17005" :@"还车时间格式错误",
    @"17006" :@"取车经纬度格式错误",
    @"17007" :@"还车经纬度格式错误",
    @"17008" :@"用户手机号格式错误",
    @"17009" :@"车主手机号格式错误",
    @"17011" :@"自己无法给自己分享车辆",
    @"17012" :@"该分享不存在",
    @"17013" :@"已经给该用户分享了一个权限，不能重复分享",
    @"17014" :@"该车辆同时只能存在10个分享",
    @"17015" :@"车辆远程控制最多只能分享5个",
    @"17016" :@"分享正在创建中...",
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    };
    
    return [errorDic.allKeys containsObject: self] ? errorDic[self] : @"未知错误";
}



@end
